/*
 * @Author: worldzb 
 * @Date: 2018-09-29 22:30:23 
 * @Last Modified by: worldzb
 * @Last Modified time: 2019-05-11 15:18:58
 */

// webpack2 需要引入path 使用绝对路径
const path = require('path');
const webpack=require('webpack');

var WebpackOnBuildPlugin = require('on-build-webpack');
const fs = require('fs')
const buildDir = './ext/js/dist/'

module.exports = {
    //  入口文件 
    entry: {
        background:'./src/background.js',
        inject:'./src/inject.js',
        popup:'./src/popup.js',
        options:'./src/options.js'
    },
    //devtool:'inline-source-map',
    //  输出文件
    output: {
        filename: '[name].js',
        path: __dirname + '/ext/js/dist'
    },
    //模块
    module:{
        rules:[
            {
                test:/\.js$/, //匹配.js文件//排除也就是不转换node_modules下面的.js文件
                exclude: /(node_modules|bower_components)/,//加载器  webpack2需要loader写完整 不能写babel 要写 bable-loader
                use:[
                    {loader:"babel-loader"}
                ]
            }
        ]
    },
    devServer:{
        contentBase:__dirname+'/dist'
    },
    plugins:[
        new webpack.HotModuleReplacementPlugin(),
        new WebpackOnBuildPlugin(function(stats) {
            const newlyCreatedAssets = stats.compilation.assets;
            const unlinked = [];
            fs.readdir(path.resolve(buildDir), (err, files) => {
                files.forEach(file => {
                    if (!newlyCreatedAssets[file]) {
                    fs.unlink(path.resolve(buildDir + file));
                    unlinked.push(file);
                    }
                });
                if (unlinked.length > 0) {
                    console.log('删除文件: ', unlinked);
                }
          });    
        })
    ]
}



